﻿//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.Taxonomy;
using EntityEditorLib;
using UniversityOfSouthampton.Taxonomy;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.UserCode;
using SharePointEntityProject.Contracts;
using System.Diagnostics;
using SharePointEntityProject.EntityEditorLib;

namespace SharePointEntityProject.VisualFieldMetadataEditor
{
    [ToolboxItem(false)]
    public partial class VisualFieldMetadataEditor : System.Web.UI.WebControls.WebParts.WebPart
    {
        private string _EntityEditorListName;
        [WebBrowsable(true), Category("Entity Editor Settings"), Personalizable, Description("Name of the storage list")]
        public string EntityEditorListName
        {
            get
            {

                return _EntityEditorListName;
            }
            set
            {
                _EntityEditorListName = value;
            }
        }

        private string _SiteURL;
        [WebBrowsable(true), Category("Entity Editor Settings"), Personalizable, Description("Set the site URL of the site")]
        public string SiteURL
        {
            get
            {
                return _SiteURL;
            }
            set
            {
                _SiteURL = value;
            }
        }
        EntityEditor entEd;
     //Use the listname from the other webpart
        //private IEntityEditorContract provider;
        //[ConnectionConsumer("Storage info consumer")]
        //public void InitializeProvider(IEntityEditorContract provider)
        //{
        //    this.provider = provider;
        //}

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
            if (String.IsNullOrEmpty(SiteURL) || String.IsNullOrEmpty(this.EntityEditorListName))
            {
                this.SiteURL = SPContext.Current.Site.Url;
                this.StatusMessageTxt.Text = "Configure this Webpart";
            }
            else
            {
                entEd = new EntityEditor(this.SiteURL, this.EntityEditorListName);

                //Check the metadata service is active. 

                //Call proxy to create/confirm metadata group exists for this list. 
                CheckTaxonomyGroup();

                var x = entEd.GetAllValidFieldTypes();//TODO: get types
            }

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            
        }

        /// <summary>
        /// Ensure there is a taxonomy group for this list
        /// </summary>
        private void CheckTaxonomyGroup()
        {
            try
            {
                string listNameStr = this.EntityEditorListName;
                entEd.CreateTaxonomyGroup(listNameStr);
                Debug.WriteLine("Taxonomy group for list created/exists " + listNameStr);
            }
            catch (Exception ex)
            {
                this.StatusMessageTxt.Text = ex.Message;
            }
            
             
        }


         




        protected void Page_Load(object sender, EventArgs e)
        {
           

        }

        protected void Create_Click(object sender, EventArgs e)
        {
            string typeStr = "URL";
            //Take txt and create
            //TODO: validate#
            try
            {
                //Take form data and make a field
                FieldInfo field = new FieldInfo()
                {

                    Name = FieldNameTxtBox.Text,
                    Description = FieldDescTxtBox.Text,
                    Group = Properties.Resources.DefaultEntityEditorContentTypeGroupName,
                   // Type = "TaxonomyFieldTypeMulti",// "TaxonomyFieldType",//TODO: resource
                    MetadataSetName = MetadataNameTxtBox.Text,

                    MetadataSetDescription = MetadataDescTxtBox.Text
                };
                    

                entEd.CreateNewField(field);
            }
            catch (Exception ex)
            {
                StatusMessageTxt.Text = ex.Message;
                return;
            }

            StatusMessageTxt.Text = "Created";
        }

        

    }
     

   
}
